import com.NoteDao.NoteSqlDao;
import com.jiazhong.Student;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.log4j.PropertyConfigurator;
import org.junit.jupiter.api.Test;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Properties;
import java.util.Scanner;

public class NoteTestDao {

        //链接
    private static SqlSession sqlSession;
    static {
        InputStream resourceAsStream = null;
        try {
            resourceAsStream = Resources.getResourceAsStream("Mybatis_comfig.xml");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        //从配置文件中获取一个工厂对象
//        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(resourceAsStream);

        //通过工厂获取sqlsession对象
        sqlSession = factory.openSession();

    }
    //日志
    public static void rizhi() throws IOException {
        Properties properties = new Properties();
        FileInputStream fileInputStream = new FileInputStream("src/main/resources/log4j.properties");
        properties.load(fileInputStream);
        PropertyConfigurator.configure(properties);
    }
    //使用注解接口实现【查询】方法
    public static void selectByNote() throws IOException {
        rizhi();
        NoteSqlDao sqlSessionMapper = sqlSession.getMapper(NoteSqlDao.class);
        List<Student> studentList = sqlSessionMapper.selectAll();
        for (Student student : studentList) {
            System.out.println("student = " + student);
        }

    }

    public static void insertByNote() throws IOException {
       rizhi();
        Student student = new Student();
        student.setSname("张艺为");
        student.setSsex("女");
        student.setSage(23);
        student.setSdepartment("lol");
        NoteSqlDao mapper = sqlSession.getMapper(NoteSqlDao.class);
        mapper.insert(student);
        sqlSession.commit();

    }

    public static void updataByNote() throws IOException {
        rizhi();
        Student student = new Student();
        student.setSname("张艺为");
        student.setSsex("变男了");
        student.setSage(23);
        student.setSdepartment("瓦罗兰特");
        NoteSqlDao mapper = sqlSession.getMapper(NoteSqlDao.class);
        mapper.update(student);
        sqlSession.commit();
    }


}
